$(function () {
    $("#jqGrid").jqGrid({
        url: '../sys/appUser/list',
        datatype: "json",
        colModel: [
            { label: 'userId', name: 'userId', index: 'userId', width: 80, key: true ,hidden:true},
            { label: '姓名', name: 'username', index: 'username', width: 80 },
            { label: '手机号', name: 'phone', index: 'phone', width: 80 },
            { label: 'appId', name: 'appId', index: 'appId', width: 80 },
            { label: '实名状态', name: 'realAuth', index: 'realAuth', width: 80,formatter:function (value, options, rowObject) {
                if (value == 0)
                    return '<span class="label label-warning">未实名</span>';
                else if (value == 1)
                    return '<span class="label label-success">已提交</span>';
                else if (value == 2)
                    return '<span class="label label-success">实名拒绝</span>';
                else if (value == 3)
                    return '<span class="label label-success">实名通过</span>';
                else if (value == -1)
                    return '<span class="label label-danger">黑名单</span>';
                return '<span class="label label-danger">未知信息</span>';
            }},
            { label: '账户状态', name: 'sataus', index: 'sataus', width: 80,formatter:function (value,options,rowObject) {
                if (value == 0)
                    return '<span class="label label-success">正常</span>';
                else if (value == 1)
                    return '<span class="label label-danger">锁定</span>'
                return '<span class="label label-danger">未知信息</span>';
            }},
            { label: '实名方式', name: 'authType', index: 'authType', width: 80,formatter:function (value, options, rowObject) {
                if (value == 0)
                    return '<span class="label label-warning">未实名</span>';
                else if (value == 1)
                    return '<span class="label label-success">自动实名</span>';
                else if (value == 2)
                    return '<span class="label label-success">人工实名</span>';
                return '<span class="label label-danger">未知信息</span>';
            }},
            { label: '注册时间', name: 'createDate', index: 'createDate', width: 120}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.loadApps();
    $("#select_type").change(function () {
        var selectValue = $(this).val();
        var urlStr = "";
        if (selectValue == "1") {
            urlStr = "../sys/appUser/list";
            vm.appId = null;
        } else{
            urlStr = "../sys/appUser/list?appId="+selectValue;
            vm.appId = selectValue;
        }
        $("#jqGrid").jqGrid("setGridParam",{
            url:urlStr
        }).trigger("reloadGrid");
    });

    $("#photoImage").click(function () {
        $("#bannerImage").click();
    });

    $("#bannerImage").change(function () {
        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
        if (objUrl != null) {
            $("#photoImage").attr("src", objUrl);
            vm.banner.image = objUrl;
        }
    });
});

function getObjectURL(file) {
    var url = null;
    // 下面函数执行的效果是一样的，只是需要针对不同的浏览器执行不同的 js 函数而已
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

var vm = new Vue({
    el: "#rrapp",
    data:{
        showList:true,
        showInfo:false,
        title:"",
        appId:null,
        userGrade:{},
        user:{},
        apps:[]
    },
    methods:{
        loadApps:function () {
            $.ajax({
                url:"/sys/app/list?page=1&limit=1000",
                type:"POST",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 0){
                        vm.apps = result.data.list;
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("error",e);
                    alert("请求失败，请重试")
                }
            })
        },
        query:function () {
            var urlStr = "";
            if (vm.appId != null){
                urlStr = "../sys/appUser/list?appId="+vm.appId;
                var search = $("#searchContent").val();
                if (search.length > 0){
                    urlStr += "&search="+search;
                }
            }else {
                urlStr = "../sys/appUser/list";
                var search = $("#searchContent").val();
                if (search.length > 0){
                    urlStr += "?search="+search;
                }
            }
            $("#jqGrid").jqGrid("setGridParam",{
                url:urlStr
            }).trigger("reloadGrid");
        },
        info:function () {
            vm.showList = false;
            vm.showInfo = true;
            // vm.user = $("#jqGrid").jqGrid().getRowData(getSelectedRow());
            $.ajax({
                url:"../sys/appUser/info/"+getSelectedRow(),
                type:"GET",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 0){
                        vm.user = result.data;
                        vm.appGrade();
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    alert("请求失败，请重试");
                    console.log("error",e);
                }
            })
        },
        removeLock:function () {
            $.ajax({
                url:"../sys/appUser/removeLock/"+vm.user.userId,
                type:"GET",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 0){
                        alert("操作成功");
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("error",e);
                    alert("请求失败，请重试");
                }
            })
        },
        removeBlackList:function () {
            $.ajax({
                url:"../sys/appUser/removeAuthBlack/"+vm.user.userId,
                type:"GET",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 0){
                        alert("操作成功");
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("error",e);
                    alert("请求失败，请重试");
                }
            })
        },
        appGrade:function () {
            $.ajax({
                url:"../sys/appUser/appGrade/"+vm.user.appGradeId,
                type:"GET",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 0){
                        vm.userGrade = result.data;
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("error",e);
                    alert("请求失败，请重试");
                }
            });
        },
        updatePhone:function () {
            var phone = window.prompt("请输入手机号");
            if (phone.length != 11)
                return;
            $.ajax({
                url:"../sys/appUser/updatePhone/"+vm.user.userId+"?phone="+phone,
                type:"GET",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 0){
                        alert("操作成功");
                        vm.reload();
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("error",e);
                    alert("请求失败，请重试");
                }
            })
        },
        resetTransPwd:function () {
            window.confirm("重置密码将会由系统随机生成密码，将密码发送至用户当前手机号。",function () {
                $.ajax({
                    url:"/sys/appUser/resetTransPwd/"+vm.user.userId,
                    type:"GET",
                    dataType:"JSON",
                    success:function (result) {
                        if (result.code == 0){
                            alert("操作成功，新密码已经发送到用户手机");
                            vm.reload();
                        }else
                            alert(result.msg);
                    },
                    error:function (e) {
                        console.log("error",e);
                        alert("请求失败，请重试");
                    }
                })
            })

        },
        resetLoginPwd:function () {
            window.confirm("重置密码将会由系统随机生成密码，将密码发送至用户当前手机号。",function () {
                $.ajax({
                    url:"/sys/appUser/resetLoginPwd/"+vm.user.userId,
                    type:"GET",
                    dataType:"JSON",
                    success:function (result) {
                        if (result.code == 0){
                            alert("操作成功，新密码已经发送到用户手机");
                            vm.reload();
                        }else
                            alert(result.msg);
                    },
                    error:function (e) {
                        console.log("error",e);
                        alert("请求失败，请重试");
                    }
                })
            })
        },
        saveOrUpdate:function () {
            Vue.delete(this.banner,'bannerImage');
            var urlStr = this.banner.bannerId == null ? "/sys/appBanner/save" : "/sys/appBanner/update";
            $.ajaxFileUpload({
                url: urlStr,
                data: JSON.parse(JSON.stringify(vm.banner)),
                type: "POST",
                contentType: "multipart/form-data",
                secureuri: false,
                fileElementId: 'bannerImage',
                dataType: 'json',
                success: function (result) {
                    if (result.code == 0) {
                        vm.reload();
                        alert("操作成功");
                    } else {
                        alert(result.msg);
                        if (this.lecture.id == null) {
                            $("#photoImage").attr("src", "");
                        }
                    }
                },
                complete: function () {
                    $("#photo").replaceWith('<input type="file" style="display: none;" accept="image/*" name="photo" id="photo" class="form-control" placeholder="file" />');
                    $("#photo").change(function () {
                        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
                        if (objUrl != null) {
                            $("#photoImage").attr("src", objUrl);
                            vm.banner.image = objUrl;
                        }
                    });
                },
                error: function (e) {
                    console.log("error", e);
                    alert("请求失败，请重试");
                }
            });
        },
        reload:function () {
            vm.showList = true;
            vm.showInfo = false;
            $("#jqGrid").jqGrid().trigger("reloadGrid");
        }
    }
});